/**
 * 全局配置文件
 * 用于解决前台路径和后台接口的硬编码问题
 */
window.AppConfig = {
    // 动态获取应用上下文路径
    getContextPath: function() {
        // 优先使用服务器端设置的CONTEXT_PATH
        if (window.CONTEXT_PATH) {

            return window.CONTEXT_PATH;
        }
        // 如果没有设置，则从URL中动态提取
        var pathname = window.location.pathname;
        var parts = pathname.split('/');
        if (parts.length > 1 && parts[1]) {
            return '/' + parts[1];
        }
        return '';
    },
    
    // 获取基础URL
    getBaseUrl: function() {

        console.log(this.getContextPath());
        return this.getContextPath();
    },
    
    // 构建完整的页面路径
    buildPageUrl: function(path) {
        return this.getBaseUrl() + '/page' + (path.startsWith('/') ? path : '/' + path);
    },
    
    // 构建完整的API路径
    buildApiUrl: function(path) {
        // 确保API路径不包含/page前缀
        var cleanPath = path.startsWith('/') ? path : '/' + path;
        return this.getBaseUrl() + cleanPath;
    },
    
    // 构建静态资源路径
    buildStaticUrl: function(path) {
        return this.getBaseUrl() + '/static' + (path.startsWith('/') ? path : '/' + path);
    },
    
    // 页面路径常量
    PAGES: {
        INDEX: '/index',
        LOGIN: '/login',
        REGISTER: '/register',
        PRODUCTS: '/products',
        PRODUCT_DETAIL: '/product-detail',
        USER_CENTER: '/user-center',
        ADMIN: '/admin',
        ADMIN_USERS: '/admin/users',
        ADMIN_PRODUCTS: '/admin/products',
        ADMIN_REVIEWS: '/admin/reviews'
        // ADMIN_DASHBOARD: '/admin/dashboard'
    },
    
    // API路径常量
    APIS: {
        USER: {
            LOGIN: '/user/login',
            LOGOUT: '/user/logout',
            REGISTER: '/user/register',
            CURRENT: '/user/current',
            UPDATE: '/user/update',
            DELETE: '/user/delete',
            ALL: '/user/list',
            COUNT: '/user/count',
            CHANGE_PASSWORD: '/user/change-password',
            RESET_PASSWORD: '/user/reset-password',
            UPDATE_STATUS: '/user/update-status',
            BATCH_DELETE: '/user/batch-delete'
        },
        PRODUCT: {
            ALL: '/product/list',
            PAGE: '/product/page',
            ADD: '/product/add',
            UPDATE: '/product/update',
            DELETE: '/product/delete',
            COUNT: '/product/count',
            CATEGORIES: '/product/categories',
            UPDATE_STATUS: '/product/update-status',
            BATCH_DELETE: '/product/batch-delete'
        },
        REVIEW: {
            ALL: '/review/list',
            ADD: '/review/add',
            DELETE: '/review/delete',
            COUNT: '/review/count',
            STATS: '/review/stats',
            AUDIT: '/review/audit',
            CHECK: '/review/check',
            USER: '/review/user',
            PRODUCT: '/review/product',
            PENDING: '/review/pending',
            BATCH_AUDIT: '/review/batch-audit',
            BATCH_DELETE: '/review/batch-delete'
        }
    }
};

// 全局便捷方法
window.buildPageUrl = function(path) {
    return AppConfig.buildPageUrl(path);
};

window.buildApiUrl = function(path) {
    return AppConfig.buildApiUrl(path);
};

window.buildStaticUrl = function(path) {
    return AppConfig.buildStaticUrl(path);
};

// 页面跳转便捷方法
window.goToPage = function(pagePath) {
    window.location.href = AppConfig.buildPageUrl(pagePath);
};

// AJAX请求便捷方法
window.apiRequest = function(options) {
    var defaultOptions = {
        type: 'GET',
        dataType: 'json',
        contentType: 'application/json',
        error: function(xhr, status, error) {
            console.error('API请求失败:', error);
            if (xhr.status === 401) {
                alert('请先登录');
                goToPage(AppConfig.PAGES.LOGIN);
            } else if (xhr.status === 403) {
                alert('权限不足');
            } else {
                alert('请求失败: ' + (xhr.responseJSON?.message || error));
            }
        }
    };
    
    // 合并选项
    var finalOptions = $.extend({}, defaultOptions, options);
    
    // 构建完整的URL
    if (finalOptions.url && !finalOptions.url.startsWith('http')) {
        finalOptions.url = AppConfig.buildApiUrl(finalOptions.url);
    }
    
    return $.ajax(finalOptions);
};